﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace SRM.Core.Utility
{
    public class CompilerExt
    {
        #region 给两个对象赋值

        private static PropertyInfo[] GetPropertyInfos(Type type)
        {
            return type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
        }

        /// <summary>
        /// 利用反射给实体对象赋值
        /// </summary>
        /// <typeparam name="S">赋值对象 包含数据的对象</typeparam>
        /// <typeparam name="T">被赋值对象 新new的空对象</typeparam>
        /// <param name="s"></param>
        /// <param name="newObject"></param>
        public static void AutoMapping<S, T>(S s, T newObject)
        {
            PropertyInfo[] pps = GetPropertyInfos(s.GetType());
            Type target = newObject.GetType();

            foreach (var pp in pps)
            {
                PropertyInfo targetPP = target.GetProperty(pp.Name);
                object value = pp.GetValue(s, null);

                if (targetPP != null && value != null)
                {
                    targetPP.SetValue(newObject, value, null);
                }
            }
        }

        #endregion 给两个对象赋值

        public static List<int> ConverToList(string Ids)
        {
            List<int> list = new List<int>();
            var strId = Ids.Split(',');
            foreach (var item in strId)
            {
                int.TryParse(item, out int id);
                list.Add(id);
            }
            return list;
        }
    }
}
